package com.kede.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kede.pay.domain.ConfigDomain;
import com.kede.pay.service.ConfigService;
import com.kede.pay.mapper.ConfigMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
* @author 11856
* @description 针对表【sy_config(系统配置)】的数据库操作Service实现
* @createDate 2023-05-06 15:08:55
*/
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, ConfigDomain>
    implements ConfigService{

    /**
     * 获取会员从当前开始下次到期时间
     * @return
     */
    @Override
    public Integer getExpiretime() {
        LambdaQueryWrapper<ConfigDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConfigDomain::getName,"expire_time");
        ConfigDomain one = getOne(queryWrapper);
        Integer day = 365;
        if (one !=null){
            day = Integer.valueOf(one.getValue());
        }
        Long l = LocalDateTime.now().plusDays(day).toEpochSecond(ZoneOffset.ofHours(8));
        return l.intValue();
    }

    /**
     * 获取会员有效期延长
     * @return
     */
    @Override
    public Integer extendExpiretime(Integer expireTime) {
        LambdaQueryWrapper<ConfigDomain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConfigDomain::getName,"expire_time");
        ConfigDomain one = getOne(queryWrapper);
        Integer day = 365;
        if (one !=null){
            day = Integer.valueOf(one.getValue());
        }
        LocalDateTime dateTime = LocalDateTime.ofEpochSecond(expireTime,0,ZoneOffset.ofHours(8));
        Long l = dateTime.plusDays(day).toEpochSecond(ZoneOffset.ofHours(8));
        return l.intValue();
    }

}




